﻿Imports System.Data.OleDb
Public Class profile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'this part gets dinerid of current user from the database. the diner id gotten from here is what is used
        'link to the view my profile page from the default page
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        Dim sql As String = "SELECT * FROM Userprofile where dinerid=@a6"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@a6", User.Identity.Name)
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()


        dr.Read()

        If Not Request.Params("dinerid") = "" Then
            If Request.Params("dinerid") = User.Identity.Name Then
                Button1.Visible = True
                Button2.Visible = True
                pic_upload.Visible = True
                btn_addnewcars.Visible = True
                ListView2.Visible = True
            Else
                Button1.Visible = False
                Button2.Visible = False
                pic_upload.Visible = False
                btn_addnewcars.Visible = False
                ListView2.Visible = False
            End If
        Else
            Response.Redirect("profile.aspx?dinerid=" & dr("dinerid"))
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim newfilename As String
        newfilename = Guid.NewGuid().ToString() & getsurfix(pic_upload.FileName)
        If pic_upload.HasFile Then
            pic_upload.SaveAs(Server.MapPath("carpictures") & "/" & newfilename)
        End If


        Dim sql As String = "INSERT INTO userpictures (pictures, dinerid, uploaddate) VALUES(@a1, @a2,@a3)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@a1", "carpictures/" & newfilename)
        cmd.Parameters.AddWithValue("@a2", User.Identity.Name)
        cmd.Parameters.AddWithValue("@a3", DateTime.Now.Date)
       
        conn.Open()

        If (cmd.ExecuteNonQuery()) Then
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            MsgBox("Picture saved!!!", MsgBoxStyle.Exclamation)
            Response.Redirect("profile.aspx?dinerid=" & Request.Params("dinerid"))
        Else
            MsgBox("Sorry, your Picture has not been saved.Check and be sure there s no mistake somewhere!!!", MsgBoxStyle.Exclamation)
            Return
        End If

    End Sub
    Function getsurfix(ByVal fn As String)
        Dim fileparts = fn.Split(".")
        Dim suffix = fileparts(fileparts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addnewcars.Click
        Response.Redirect("addnewcars.aspx")
    End Sub
End Class